﻿using System.Collections.Concurrent;

namespace LS.Screening.Infrastructure
{
    /// <summary>
    /// 类型扩展方法
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// 缓存
        /// </summary>
        private static ConcurrentDictionary<Type, DependencyAttribute> dictionary = new ConcurrentDictionary<Type, DependencyAttribute>();

        /// <summary>
        /// 获取类型的InjectOnAttribute特性
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DependencyAttribute GetInjectOnAttribute(this Type type)
        {
            if (dictionary.TryGetValue(type, out var attribute))
            {
                return attribute;
            }

            attribute = type.GetCustomAttributes(false).OfType<DependencyAttribute>().FirstOrDefault();
            dictionary.TryAdd(type, attribute);
            return attribute;
        }
    }
}
